<?php namespace App\Exceptions;

class BaseException extends \Exception
{
    const SYSTEM_ERROR_DEFAULT_CODE = 9999;
    const SYSTEM_OTHER_ERROR_DEFAULT_CODE = 9998;
    const SYSTEM_VALIDATE_ERROR_DEFAULT_CODE = 9997;
    const SYSTEM_ERROR_DEFAULT_MESSAGE = '系统错误，请重试或联系客服。';

    protected $messages = [];

    public function __construct($code)
    {
        $code = (int)$code;
        $code = $code > 0 ? $code : self::SYSTEM_ERROR_DEFAULT_CODE;
        parent::__construct($this->getMessageByCode($code), $code);
    }

    public function getMessageByCode($code)
    {
        return array_get($this->messages, $code, self::SYSTEM_ERROR_DEFAULT_MESSAGE);
    }
}
